package com.example.client_fmf;

import java.io.UnsupportedEncodingException;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MapActivity extends ActionBarActivity implements AsyncResponse{
	final private String LOCATION_PARAMETER  = "011";    
	final int CODE = 0  ; 
	final int TEXTE_MSG = 1 ; 
	final int LON = 0 ;
	final int LAT = 1 ;	
	final int HUM = 2 ;
	
	
	String myId ; 
	private Connection cnt  ; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);

		cnt = MainActivity.cnt ; 
		// cnt = (Connection) getIntent().getSerializableExtra("Connextion")  ; 
		cnt.delegate = this ; 
		//myId = MainActivity.myId ; 
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	private void SendLocation(String lon , String lat, String hum ) throws UnsupportedEncodingException
	{
		
		String location = lon+";"+ lat + ";" +hum+";" ; 
		cnt.send(formatMsg(LOCATION_PARAMETER, location));		
	
	}
	
	private String formatMsg(String code , String msg)
	{
		String formatedMsg  = code+"-"+ MainActivity.myId + "-" +msg+"-"+MainActivity.partnerId+"-" ; 
		return formatedMsg ; 
	}
	


	//Bouton refresh 
	public void getLocation_button(View view) throws UnsupportedEncodingException
	{
		
		
		SendLocation("lon" , "lat", "hum");  
	}
	
	
	public void DisplayParm(String msg)
	{
		 String[] msgTab = msg.split("-") ; 
		if(msgTab[CODE].equals(LOCATION_PARAMETER))
		{
			if(!msgTab[TEXTE_MSG].isEmpty())
			{
						
				String[] locations = msgTab[TEXTE_MSG].split(";") ; 
				String latitude  = locations[LAT] ; 
				String longitude = locations[LON] ; 
				String humidite = locations[HUM] ; 
			}
			
			TextView text = (TextView) findViewById(R.id.textView1) ; 
			text.setText(msg);
		}
	}
	
	//Receive message from server 
	
	@Override
	public void processFinish(String output) {
		
			DisplayParm(output) ; 
			
		
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK ) {
	    	Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			finish() ; 
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	
	
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		
		
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_map, container,
					false);
			return rootView;
		}
	}

}
